package com.keyancompany.zhongbaopingtai.weight;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.ProgressBar;

import com.keyancompany.zhongbaopingtai.R;

/**
 * Created by firefox on 2017/2/11.
 */

public class IListView extends ListView implements AbsListView.OnScrollListener {

    private SwipeLayoutState stateListener;
    private boolean isLoading = false;


    public void setStateListener(SwipeLayoutState stateListener) {
        this.stateListener = stateListener;
        setOnScrollListener(this);
    }



    public IListView(Context context) {
        super(context);
    }

    public IListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public IListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void onScrollStateChanged(AbsListView absListView, int i) {

    }

    @Override
    public void onScroll(AbsListView absListView, int i, int i1, int i2) {

        int lastVisibleItemPosition = getLastVisiblePosition();
        if (lastVisibleItemPosition + 1 == getAdapter().getCount()) {
            if (stateListener == null)
                return;
            if (stateListener.isRefresh() && loadMoreView != null) {
                isLoading = false;
                removeFooterView(loadMoreView);
                return;
            }

            if (stateListener.isLastPage() && loadMoreView != null) {
                isLoading = false;
                removeFooterView(loadMoreView);
                return;
            }
            if (!stateListener.isLastPage() && !isLoading) {
                isLoading = true;
                addFooterView(getLoadMoreView());
            }
        }
    }

    View loadMoreView;

    public interface SwipeLayoutState {

        boolean isRefresh();

        boolean isLastPage();

        void requestNextPage();
    }

    public void removeFooterView() {
        if (loadMoreView != null)
            removeFooterView(loadMoreView);
    }

    private View getLoadMoreView() {
        if (loadMoreView == null) {
            loadMoreView = View.inflate(getContext(), R.layout.load_more, null);
            final ProgressBar bar = (ProgressBar) loadMoreView.findViewById(R.id.progressbar);
            bar.setVisibility(View.GONE);
            loadMoreView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    bar.setVisibility(View.VISIBLE);
                    if (stateListener == null)
                        return;
                    stateListener.requestNextPage();
                }
            });
        }

        return loadMoreView;
    }

    public void loadMoreFinish() {
        if (loadMoreView != null) {
            loadMoreView.findViewById(R.id.progressbar).setVisibility(View.GONE);
        }
    }
}
